<?php 
/**
 * This file is part of DomSql.
 * 
 * DomSql is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomSql is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomSql.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* @package DomSql
*/ 
class DomSqlQueryIterator extends DomSqlIterator {
	
	/**
	 * Query object
	 *
	 * @var DomSqlQuery
	 */
	protected $queryObject;

	/**
	 * @param DomSqlQuery $query
	 * @param String $alias
	 * @return DomSqlQueryIterator
	 */
	public static function c( DomSqlQuery $query, $alias = DomSql::DEFAULT_ALIAS ) {
		$class = get_called_class();
		return new $class( $query, $alias );
	}
	/**
	 * @param DomSqlQuery $query
	 * @param String $alias
	 */
	public function __construct( DomSqlQuery $query, $alias = DomSql::DEFAULT_ALIAS ) {
		parent::__construct( $query->toString(), $alias);
		$this->queryObject = $query;
		$this->setIndexColumnName( $query->indexColumnName );
		$this->setValueColumnName( $query->valueColumnName );
	}
	/**
	 * Returns nr of possible results for this query without LIMIT clause.
	 * Depending on query it uses FOUND_ROWS() or sends another count(*) query.
	 * @return Int
	 */
	public function foundRows() {	
		if( $this->foundRows === NULL ) {
			if( $this->queryObject->sqlCalcFoundRows === true)
				$this->foundRows = parent::foundRows();
			else
				$this->foundRows = (int)DomSql::selectOneQuery( $this->queryObject->toCountString() ,'n', $this->queryObject->alias );
		}
		return $this->foundRows;
	}

}
?>